<?php

namespace App\Admin\Controllers;

use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Spatie\Permission\Models\Role;

class UserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('前台用户');
            $content->description('...');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新建用户');
            $content->description('...');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            //$grid->paginate(15);
            /*$grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
            });*/
            $grid->filter(function($filter){

                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 在这里添加字段过滤器
                $filter->like('name', 'name');

            });
            //$grid->disableCreateButton();
            $grid->id('ID')->sortable();
            $grid->headpic('头像')->image('/uploads/', 36, 36);
            $grid->name('姓名')->badge();
            /*$grid->roles('角色')->display(function ($role){
                return $role[0]['name'];
            });*/
            $grid->roles('角色')->pluck('name')->label();
            $grid->sex('性别')/*->display(function ($sex) {
                return $sex==0?'不详':($sex==1?'男':'女');
            })*/->editable('select', [0 => '不详', 1 => '男', 2 => '女']);
            /*$states = [
                'on'  => ['value' => 1, 'text' => '打开', 'color' => 'primary'],
                'off' => ['value' => 2, 'text' => '关闭', 'color' => 'default'],
            ];
            $grid->sex()->switch($states);*/
            $grid->email('邮箱')->display(function ($email){
                return "<span style='color: #999;'><i class='fa fa-envelope'></i> $email</span>";
            });
            /*$grid->headpic('头像')->display(function ($headpic) {
                $url = asset('storage/headpic/'.$headpic);
                return "<img style='width: 34px;height: 34px' src='$url'>";
            });*/
            $grid->created_at('创建于');
            $grid->updated_at('更新于');
            $grid->filter(function ($filter) {
                // 设置created_at字段的范围查询
                $filter->between('created_at', 'Created Time')->datetime();
            });
        });
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {
            $form->tab('Basic info', function ($form) {
                $form->hidden('id', 'ID');
                $form->radio('sex', 'Sex')->options([0 => '不详', 1 => '男', 2 => '女']);
                $form->text('name', 'Name')->rules('required|max:10');
                $form->image('headpic', 'Head Pic')->uniqueName()->removable();
                $form->hidden('created_at', 'Created At');
                $form->hidden('updated_at', 'Updated At');
                $form->select('roles', trans('admin.roles'))->options(Role::all()->pluck('name', 'id'));
            })->tab('Login Info', function ($form) {
                $form->email('email', 'E-mail')->rules('required|max:20');
                $form->password('password', 'Password')->rules('required|confirmed');
                $form->password('password_confirmation', 'Password Confirmation')->rules('required')
                    ->default(function ($form) {
                        return $form->model()->password;
                    });
                $form->ignore(['password_confirmation']);
            });

            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }
            });
            /*$form->tools(function (Form\Tools $tools) {
                // 去掉返回按钮
                $tools->disableBackButton();

                // 去掉跳转列表按钮
                $tools->disableListButton();

                // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
                $tools->add('<a class="btn btn-sm btn-danger"><i class="fa fa-trash"></i>&nbsp;&nbsp;delete</a>');
            });*/
        });
    }
}
